package com.gzbugu.bugusys.action;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import com.opensymphony.xwork2.ActionSupport;
import com.gzbugu.bugusys.util.PageBean;
import com.gzbugu.bugusys.vo.CoCertificate;
import com.gzbugu.bugusys.dao.IBaseDAO;
import com.gzbugu.bugusys.service.ICoCertificateService;

@Controller
public class CoCertificateAction extends ActionSupport {
	private CoCertificate coCertificate;
	private Integer coCertificateId;
	private String coCertificateName;
	private Short coCertificateTypeId;
	private List listCoCertificate;
	private File picture;
	private String pictureContentType;
	private String pictureFileName;
	private Integer page;// 第几页
	private PageBean pageBean;// 包含分布信息的bean
	@Resource(name = "coCertificateService")
	private ICoCertificateService coCertificateService;
	@Resource(name = "baseDAO")
	private IBaseDAO baseDAO;

	// 增加
	@Transactional
	public String save() throws Exception {

		/*
		 * if(picture==null||coCertificate.getCoCertificateName()==null||coCertificate
		 * .getCoCertificateIssueDate()==null||coCertificate.
		 * getCoCertificateIssueUnit()==null) return ERROR;
		 */
		File coCertificateFiles = this.coCertificateService.saveUploadPicture(
				pictureFileName, picture);
		if (coCertificateFiles.exists()) {
			this.coCertificate.setUrl(coCertificateFiles.getAbsolutePath());
		}
		coCertificate.setUploadDate(new Date());
		baseDAO.save(this.coCertificate);

		return SUCCESS;

	}

	// 查询
	public String findAll() {
		this.pageBean = this.coCertificateService.queryForPage(10,
				page == null ? 1 : page);
		return SUCCESS;
	}

	public String findByHQL() {
		this.pageBean = this.coCertificateService.queryForPageHQL(10,
				page == null ? 1 : page, coCertificateTypeId);
		return SUCCESS;
	}

	// 删除
	@Transactional
	public String delete() {
		coCertificate = (CoCertificate) baseDAO.findById(coCertificateId,
				com.gzbugu.bugusys.fields.VoClasseFields.CO_CERTIFICATE);
		if (null != coCertificate)
			baseDAO.delete(coCertificate);
		return SUCCESS;
	}

	// 修改
	@Transactional
	public String update() {
		CoCertificate newCoCertificate = (CoCertificate) baseDAO.findById(
				coCertificateId,
				com.gzbugu.bugusys.fields.VoClasseFields.CO_CERTIFICATE);
		if (null != picture) {
			File coCertificateFiles = this.coCertificateService
					.saveUploadPicture(pictureFileName, picture);
			if (coCertificateFiles.exists()) {
				this.coCertificate.setUrl(coCertificateFiles.getAbsolutePath());
			}
		}
		if (null != newCoCertificate) {
			newCoCertificate.setName(coCertificate.getName());
			newCoCertificate.setIssueDate(coCertificate.getIssueDate());
			newCoCertificate.setIssueUnit(coCertificate.getIssueUnit());
			newCoCertificate.setValidityPeriod(coCertificate
					.getValidityPeriod());
			newCoCertificate.setType(coCertificate.getType());
			newCoCertificate.setUrl(coCertificate.getUrl());
			baseDAO.attachDirty(newCoCertificate);
		}

		return SUCCESS;
	}

	public String findById() {
		coCertificate = (CoCertificate) baseDAO.findById(coCertificateId,
				com.gzbugu.bugusys.fields.VoClasseFields.CO_CERTIFICATE);
		return SUCCESS;
	}

	public CoCertificate getCoCertificate() {
		return coCertificate;
	}

	public void setCoCertificate(CoCertificate coCertificate) {
		this.coCertificate = coCertificate;
	}

	public ICoCertificateService getCoCertificateService() {
		return coCertificateService;
	}

	public void setCoCertificateService(
			ICoCertificateService coCertificateService) {
		this.coCertificateService = coCertificateService;
	}

	public List getListCoCertificate() {
		return listCoCertificate;
	}

	public void setListCoCertificate(List listCoCertificate) {
		this.listCoCertificate = listCoCertificate;
	}

	public Integer getCoCertificateId() {
		return coCertificateId;
	}

	public void setCoCertificateId(Integer coCertificateId) {
		this.coCertificateId = coCertificateId;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public File getPicture() {
		return picture;
	}

	public void setPicture(File picture) {
		this.picture = picture;
	}

	public String getPictureContentType() {
		return pictureContentType;
	}

	public void setPictureContentType(String pictureContentType) {
		this.pictureContentType = pictureContentType;
	}

	public String getPictureFileName() {
		return pictureFileName;
	}

	public void setPictureFileName(String pictureFileName) {
		this.pictureFileName = pictureFileName;
	}

	public String getCoCertificateName() {
		return coCertificateName;
	}

	public void setCoCertificateName(String coCertificateName) {
		this.coCertificateName = coCertificateName;
	}

	public Short getCoCertificateTypeId() {
		return coCertificateTypeId;
	}

	public void setCoCertificateTypeId(Short coCertificateTypeId) {
		this.coCertificateTypeId = coCertificateTypeId;
	}

}
